package com.sisy.demo.config;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ExecutorService;

/**
 * @Description: 消息队列配置
 * @author: xx
 * @date: 2020-06-12 09:43
 * @version: 0.0.1
 * @Copyright: 2020 www.jztdata.com Inc. All rights reserved.
 * 注意：本内容仅限于杭州金智塔科技有限公司内部传阅，禁止外泄以及用于其他的商业目
 */
@Configuration
public class RabbitMQConfig {
    @Bean
    public DirectExchange directExchange(){
        return new DirectExchange("rpc_direct");
    }

    @Bean
    public Queue autoDeleteQueue1(){
        return new Queue("rpc_queue");
    }

    @Bean
    public Binding binding1(DirectExchange rpc, Queue autoDeleteQueue1){
        return BindingBuilder.bind(autoDeleteQueue1).to(rpc).with("rpc");
    }


//    @Bean
//    public TopicExchange topic() {
//        return new TopicExchange("blog_topic");
//    }
//
//    @Bean
//    public Queue autoDeleteQueue1(){
//        return new AnonymousQueue();
//    }
//
//    @Bean
//    public Queue autoDeleteQueue2(){
//        return new AnonymousQueue();
//    }
//
//    @Bean
//    public Binding binding1(TopicExchange topic, Queue autoDeleteQueue1){
//        return BindingBuilder.bind(autoDeleteQueue1).to(topic).with("*.orange.*");
//    }
//
//    @Bean
//    public Binding binding2(TopicExchange topic, Queue autoDeleteQueue2){
//        return BindingBuilder.bind(autoDeleteQueue2).to(topic).with("*.*.rabbit");
//    }
//
//    @Bean
//    public Binding binding3(TopicExchange topic, Queue autoDeleteQueue2){
//        return BindingBuilder.bind(autoDeleteQueue2).to(topic).with("lazy.#");
//    }


//    @Bean
//    public DirectExchange direct() {
//        return new DirectExchange("blog_direct");
//    }
//
//    @Bean
//    public Queue autoDeleteQueue1(){
//        return new AnonymousQueue();
//    }
//
//    @Bean
//    public Queue autoDeleteQueue2(){
//        return new AnonymousQueue();
//    }
//
//    @Bean
//    public Binding binding1(DirectExchange direct, Queue autoDeleteQueue1){
//        return BindingBuilder.bind(autoDeleteQueue1).to(direct).with("black");
//    }
//
//    @Bean
//    public Binding binding2(DirectExchange direct, Queue autoDeleteQueue2){
//        return BindingBuilder.bind(autoDeleteQueue2).to(direct).with("black");
//    }
//
//    @Bean
//    public Binding binding3(DirectExchange direct, Queue autoDeleteQueue2){
//        return BindingBuilder.bind(autoDeleteQueue2).to(direct).with("green");
//    }

//    @Bean
//    public FanoutExchange fanout(){
//        return new FanoutExchange("blog_fanout");
//    }
//
//    @Bean(name = "q01")
//    public Queue autoDeleteQueue1() {
//        return new AnonymousQueue();
//    }
//
//    @Bean(name = "q02")
//    public Queue autoDeleteQueue2() {
//        return new AnonymousQueue();
//    }
//
//    @Bean(name = "b01")
//    public Binding binding1(FanoutExchange fanout,@Qualifier("q01") Queue autoDeleteQueue1){
//        return BindingBuilder.bind(autoDeleteQueue1).to(fanout);
//    }
//
//    @Bean(name = "b02")
//    public Binding binding2(FanoutExchange fanout,@Qualifier("q02")Queue autoDeleteQueue2){
//        return BindingBuilder.bind(autoDeleteQueue2).to(fanout);
//    }

//    @Bean(name = "q2801")
//    public Queue queueBuild1(){
//        return new Queue("queue_2801");
//    }
//
//
//    @Bean(name = "q2802")
//    public Queue queueBuild2(){
//        return new Queue("queue_2802");
//    }
//
//    @Bean
//    public DirectExchange exchangeBuild(){
//        return new DirectExchange("testExchange_28001");
//    }
//
//    @Bean(name = "b2801")
//    public Binding bindingBuild1( @Qualifier("q2801")Queue queue, DirectExchange exchanges) {
//        return BindingBuilder.bind(queue).to(exchanges).with("demo_key_2801");
//    }
//
//    @Bean(name = "b2802")
//    public Binding bindingBuild2( @Qualifier("q2802")Queue queue, DirectExchange exchanges) {
//        return BindingBuilder.bind(queue).to(exchanges).with("demo_key_2802");
//    }
//
//    @Bean(name = "durable")
//    public Queue queueBuild2(){
//        return new Queue("blog_queue_durable", true);
////        new Queue("");
//    }
//
//    @Bean(name = "undurable")
//    public Queue queueBuild3(){
//        return new Queue("blog_queue_undurable",false);
//    }

    @Bean
    public ConnectionFactory connectionFactory(){
        CachingConnectionFactory factory = new CachingConnectionFactory();
        factory.setUri("amqp://sisy:hdljd911@139.9.77.235:18081");
        return factory;
    }

    @Bean("workListenerFactory")
    public RabbitListenerContainerFactory<?> rabbitListenerContainerFactory(ConnectionFactory connectionFactory){
        //SimpleRabbitListenerContainerFactory发现消息中有content_type有text就会默认将其转换成string类型的
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setPrefetchCount(1);
        return factory;
    }
}
